// RealPlay Ltd  copyrights.
// This program is Open Source AsIs
// You may use it for all purposes, including commercial
// But!!!
// If you use any part of this code, you must state clearly
// both in your source file and in the Help|about of your program,
// that the origin program or code was built by RealPlay Ltd

// ***********************************************************************/
// *                                                                     */
// *     This unit is the Auto UpDate (FTP) for "Paint" Program          */
// *                                                                     */
// ***********************************************************************/

unit UPUpdate;

interface

uses Classes, windows, SysUtils, Forms,
{$IFDEF _DELPHI2010}
  IdFTPCommon, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdExplicitTLSClientServerBase,
{$ENDIF}
  IdFTP;

Type
  TUpdate = class(TThread)
    ftp: TIdFTP;
    CommandResult: String;
    ListResults: TStrings;
    procedure Execute; override;
    procedure createftp;
    procedure FtpConnect(Sender: TObject);
    procedure FtpDisconnect(Sender: TObject);
  public
    procedure Disconnect;
  end;

var
  ExePath: string;
  UpDateOn: boolean = true;
  RunUpdate: boolean = false;
  verRealPlay: string = '0.001';

  // ***********************************************************************
implementation

var
  ftpConnected: boolean = false;
  LastCmd: boolean = false;
  NewVer: string = '0';

procedure TUpdate.Disconnect;
begin
  ftp.Disconnect;
end;

procedure TUpdate.FtpDisconnect(Sender: TObject);
begin
  ftpConnected := false;
end;

procedure TUpdate.FtpConnect(Sender: TObject);
begin
  ftpConnected := true;
end;

procedure TUpdate.Execute;
Label endftp;
begin
  createftp;
  FreeOnTerminate := true;
  self.Priority := tpLower;
  sleep(0);
  ftp.Connect;
  if ftpConnected then
  begin
    ftp.ChangeDir('\anonftp\pub\Download');
{$IFDEF _DELPHI2010}
    CommandResult := ftp.LastCmdResult.Code;
{$ELSE}
    CommandResult := ftp.CmdResult;
{$ENDIF}
    if (pos('2', CommandResult) = 1) then
    begin
      if not fileexists(ExePath + 'PaintUpdate.exe') then
      begin
        ftp.Get('PaintUpdate.exe', ExePath + 'PaintUpdate.exe');
{$IFDEF _DELPHI2010}
        CommandResult := ftp.LastCmdResult.Code;
{$ELSE}
        CommandResult := ftp.CmdResult;
{$ENDIF}
        if (pos('2', CommandResult) = 1) then
        begin
{$IFDEF _DELPHI2010}
          ftp.list;
          CommandResult := ftp.LastCmdResult.Code;
{$ELSE}
          ftp.list(ListResults);
          CommandResult := ftp.CmdResult;
{$ENDIF}
          if (pos('2', CommandResult) = 1) then
          begin
{$IFDEF _DELPHI2010}
            if (ftp.ListResult.IndexOf('verRealPlay') > 0) then
              NewVer := ftp.ListResult[ftp.ListResult.IndexOf('verRealPlay')];
{$ELSE}
            if (ListResults.IndexOf('verRealPlay') > 0) then
              NewVer := ListResults[ListResults.IndexOf('verRealPlay')];
{$ENDIF}
            If verRealPlay < NewVer then
            begin
              ftp.Get('Paint.exe', ExePath + 'Paint.ex_');
{$IFDEF _DELPHI2010}
              CommandResult := ftp.LastCmdResult.Code;
{$ELSE}
              CommandResult := ftp.CmdResult;
{$ENDIF}
              if (pos('2', CommandResult) = 1) then
                RunUpdate := true;
            end;
          end;
        end;
      end;
    end;
  end;
  UpDateOn := false;
  Disconnect;
end;

// -----------------------------------------------------------------------
procedure TUpdate.createftp;
begin

  ftp := TIdFTP.Create(application.MainForm);
  with ftp do
  begin
    Name := 'ftp';
    Host := 'www.therealplay.com';
    Passive := false;
    Password := 'm!eh8mB4Qp!';
    TransferType := ftBinary;
    Port := 21;
    Tag := 0;
{$IFDEF _DELPHI2010}
    username := 'w00218843';
{$ELSE}
    user := 'w00218843';
{$ENDIF}
    /// //EVENTS
    OnConnected := FtpConnect;
    OnDisconnected := FtpDisconnect;
  end;
end;

// -----------------------------------------------------------------------
initialization

ExePath := ExtractFilePath(application.ExeName);

end.
